package com.es.common.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import com.es.common.cache.CacheStoreFactory;
import com.es.common.cache.GlobalCacheName;

@Component
public class SpringUtils implements ApplicationContextAware{

	private static ApplicationContext context;
	
	public void setApplicationContext(ApplicationContext context)
			throws BeansException {
		if (SpringUtils.context == null) {
			SpringUtils.context = context;
		}
	}
	public static ApplicationContext getContext() {
		return context;
	}
	public static <T>  T getBean(Class<T> clazz) {
		return getContext().getBean(clazz);
	}
	
	public static <T> T getBean(String name, Class<T> clazz){
		return getContext().getBean(name, clazz);
	}
	/**
	 * 获取缓存工厂
	 * @return
	 */
	public static CacheStoreFactory getCacheFactory(){
		return getBean(GlobalCacheName.CACHE.getName(), CacheStoreFactory.class);
	}

}
